import commJS from '../common.js';
import richText from '../../../src/page/common/richText';

export default {
  components: {
    richText
  },
  data() {
    return {
      //  表单
      form_data: {
        title: '',
        descp: '',
        image: [],
        images: [],
        logo:[],
        base_hits:'',
        duration: "",
      },

      rules: {
        title: [{
          required: true,
          message: '请输入电子刊名称',
          trigger: 'blur',
        }],
        descp: [{
          required: true,
          message: '请输入电子刊描述',
          trigger: 'blur',
        }],
        image: [{
          required: true,
          message: '请上传电子刊封面',
          trigger: 'blur',
          type: 'array',
          min: 1
        }],
        images: [{
          required: true,
          message: '请上传电子刊内容页',
          trigger: 'blur',
          type: 'array',
          min: 1
        }],
      },

      // 七牛云地址
      upload_img_url: this.adminApi.upload_url,
      postData: {},
      domain: '',

      smallLesson_id: '',


      src: "",
      is_upload_video: false,
      preview_video_dialog: false,
      loading: '',
      music: {
        isPlay: false,
        currentTime: 0,
        maxTime: 0,
        volume: 100
      }
    }
  },

  /**
   * 进入页面加载
   */
  mounted: function () {
    var that = this;
    commJS.getQiNiuToken(that);

    var query = that.$route.query;
    if (query.smallLesson_id) {
      that.smallLesson_id = query.smallLesson_id;
      that.getDetail();
    }
    //初始化播放器
    that.$nextTick(() => {
      setInterval(that.listenMusic, 1000);
    });
  },

  //方法
  methods: {
    /**
     * 获取详情
     */
    getDetail() {
      var that = this;

      that.axios.post("/ebook/edit", {
        id: that.smallLesson_id
      }).then(data => {
        if (data) {
          that.form_data.title = data.title;
          that.form_data.descp = data.descp;
          that.form_data.sort = data.sort;
          that.form_data.audio = data.audio;
          if(data.audio){
            that.is_upload_video = true;
          }

          that.form_data.image = [{
            url: data.image
          }];
          if(data.images){
            data.images.split(',').forEach(item=>{
              that.form_data.images.push({
                url:item
              })
            })
          }
          if(data.logo){
            data.logo.split(',').forEach(item=>{
              that.form_data.logo.push({
                url:item
              })
            })
          }
          that.form_data.base_hits = data.base_hits;

        }
      });
    },

    /**
     * 保存预览
     */
    save() {
      const that = this;
      that.$refs.form_data.validate((valid) => {
        if (!valid) return that.$message.warning('请完整填写内容!');
        var formData = {};

        formData.title = that.form_data.title;
        formData.descp = that.form_data.descp;
        formData.image = that.form_data.image[0].url;
        formData.base_hits = that.form_data.base_hits;
        formData.audio = that.form_data.audio;
        formData.sort = that.form_data.sort;
        formData.logo=[];
        that.form_data.logo.forEach(item=>{
          formData.logo.push(item.url)
        })
        formData.logo=formData.logo.join(',');
        formData.images=[];
        that.form_data.images.forEach(item=>{
          formData.images.push(item.url)
        })
        formData.images=formData.images.join(',');
        var url = '/ebook/create';
        if (that.smallLesson_id) {
          formData.id = that.smallLesson_id;
          url = '/ebook/update';
        }
        that.axios.post(url, formData).then(() => {

          that.$message.success(that.smallLesson_id ? '修改成功' : '添加成功');
          that.$router.go(-1)
        });
      })
    },
    handle_exceed_video() {
      this.$message.warning("只能上传一个音频!");
    },

    /**
     * 上传音频-成功
     */
    upload_video_success(e) {
      let that = this;
      that.form_data.audio = that.domain + e.key;
      that.is_upload_video = true;
      that.loading = false;

      setTimeout(function () {
        that.form_data.duration = Math.floor(that.$refs.music.duration);
      }, 700)
    },

    /**
     * 上传前
     */
    before_upload() {
      this.loading = true;
    },

    /**
     * 移除
     */
    remove_audio(e) {
      let that = this;

      that.$confirm('是否删除该项?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {

        that.form_data.video = "";
        that.is_upload_video = false;

      }).catch();
    },

    /**
     * 富文本改变时
     */
    editor_change(e) {
      this.form_data.content = e;
    },

    /**
     * 图片上传成功
     */
    img_succ(res) {
      const that = this;
      that.form_data.image.push({
        url: that.domain + res.key,
      })
    },

    /**
     *图片移除
     */
    del_img(file, fileList) {
      this.form_data.image = fileList
    },

    /**
     * 文件超出个数限制时的钩子
     */
    descExceed() {
      this.$message.warning('只能上传一张图片哦!');
    },

    /**
     * 图片上传成功
     */
    img_succ2(res) {
      const that = this;
      that.form_data.logo.push({
        url: that.domain + res.key,
      })
    },

    /**
     *图片移除
     */
    del_img2(file, fileList) {
      this.form_data.logo = fileList
    },

    /**
     * 文件超出个数限制时的钩子
     */
    descExceed2() {
      this.$message.warning('只能上传9张图片哦!');
    },
    /**
     * 图片上传成功
     */
    img_succ3(res) {
      const that = this;
      that.form_data.images.push({
        url: that.domain + res.key,
      })
    },

    /**
     *图片移除
     */
    del_img3(file, fileList) {
      this.form_data.images = fileList
    },

    /**
     * 文件超出个数限制时的钩子
     */
    descExceed3() {
      this.$message.warning('只能上传9张图片哦!');
    },
    /**
     * 返回
     */
    back() {
      this.$router.go(-1);
    },
    /**
     * 播放器
     */
    listenMusic() {
      if (!this.$refs.music) {
        return;
      }
      if (this.$refs.music.readyState) {
        this.music.maxTime = this.$refs.music.duration;
      }
      this.music.isPlay = !this.$refs.music.paused;
      this.music.currentTime = this.$refs.music.currentTime;
    },

    /**
     * 播放/暂停音乐
     */
    play() {
      if (this.$refs.music.paused) {
        this.$refs.music.play();
      } else {
        this.$refs.music.pause();
      }
      this.music.isPlay = !this.$refs.music.paused;
      this.$nextTick(() => {
        document.getElementById("play").blur();
      });
    },

    /**
     * 格式化时间
     */
    formatTime(time) {
      let it = parseInt(time);
      let m = parseInt(it / 60);
      let s = parseInt(it % 60);
      return (m < 10 ? "0" : "") + parseInt(it / 60) + ":" + (s < 10 ? "0" : "") + parseInt(it % 60)
    },

    /**
     * 改变播放时间
     */
    changeTime(time) {
      this.$refs.music.currentTime = time;
    },

    /**
     * 调整音量
     */
    changeVolume(v) {
      this.music.volume += v;
      if (this.music.volume > 100) {
        this.music.volume = 100;
      }
      if (this.music.volume < 0) {
        this.music.volume = 0;
      }
      this.$refs.music.volume = this.music.volume / 100;
    }
  }
}
